package com.haoran.dwz.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;

import com.google.gson.Gson;
import com.haoran.dwz.service.DwzService;
import com.haoran.dwz.service.impl.DwzServiceRetrofitImpl;
import com.haoran.openapi.dwz.DwzServiceAPI;

import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

@Configuration
@Profile(value = { "retrofit" })
public class RetrofitConfig {

	/**
	 * 后面不要加 /	
	 */
	@Value("${dwz.service.address:http://localhost:8080}")
	private String SERVICE_ADDRESS;

	@Bean
	public DwzService dwzServiceRetrofitImpl() {
		return new DwzServiceRetrofitImpl();
	}

	@Bean
	public DwzServiceAPI dwzServiceAPI(@Autowired Retrofit Retrofit) {
		DwzServiceAPI dwzServiceAPI = Retrofit.create(DwzServiceAPI.class);
		return dwzServiceAPI;
	}
	
	@Bean
	public Retrofit createRetrofit() {
		OkHttpClient.Builder builder = new OkHttpClient().newBuilder();
		 builder.addInterceptor(new EncryptInterceptor());
		 Retrofit retrofit = new Retrofit.Builder()
		         .baseUrl(SERVICE_ADDRESS)
//		         .addConverterFactory(ScalarsConverterFactory.create())
		         //可以接收自定义的Gson，当然也可以不传
		         .addConverterFactory(GsonConverterFactory.create())
		         // 针对rxjava2.x
//		         .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
		         .client(builder.build())
		         .build();
		 
		 return retrofit;
	}
}
